package com.insurance.modules.partner.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.insurance.common.enums.DeleteFlagEnum;
import com.insurance.entity.PartnerPermissionEntity;
import com.insurance.modules.sys.controller.AbstractController;
import com.insurance.service.PartnerPermissionService;
import com.insurance.service.PartnerRolePermissionService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.insurance.common.utils.PageUtils;
import com.insurance.common.utils.R;



/**
 * 合作方权限
 *
 * @date 2021-02-27 15:51:21
 */
@RestController
@RequestMapping("partner/partnerpermission")
public class PartnerPermissionController extends AbstractController {

    @Autowired
    private PartnerPermissionService partnerPermissionService;

    @Autowired
    private PartnerRolePermissionService partnerRolePermissionService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("partner:partnerpermission:list")
    public R list(@RequestParam Map<String, Object> params, PartnerPermissionEntity partnerPermissionEntity){
        QueryWrapper<PartnerPermissionEntity> queryWrapper = partnerPermissionService.getQueryWrapper(partnerPermissionEntity);
        PageUtils page = partnerPermissionService.queryPage(params, queryWrapper);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{permissionId}")
    @RequiresPermissions("partner:partnerpermission:info")
    public R info(@PathVariable("permissionId") String permissionId){
		PartnerPermissionEntity partnerPermission = partnerPermissionService.getById(permissionId);
        return R.ok().put("partnerPermission", partnerPermission);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("partner:partnerpermission:save")
    public R save(@RequestBody PartnerPermissionEntity partnerPermission){
        partnerPermission.setPermissionId(UUID.randomUUID().toString().replaceAll("-", ""));
        partnerPermission.setUpdatePersonId(getUserIdStr());
        partnerPermission.setUpdatePersonName(getUserName());
        partnerPermission.setUpdateTime(new Date());
        partnerPermission.setDeleteFlag(String.valueOf(DeleteFlagEnum.UN_DELETE.getCode()));
		partnerPermissionService.save(partnerPermission);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("partner:partnerpermission:update")
    public R update(@RequestBody PartnerPermissionEntity partnerPermission){
        partnerPermission.setUpdatePersonId(getUserIdStr());
        partnerPermission.setUpdatePersonName(getUserName());
        partnerPermission.setUpdateTime(new Date());
		partnerPermissionService.updateById(partnerPermission);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("partner:partnerpermission:delete")
    public R delete(@RequestBody String[] permissionIds){
        PartnerPermissionEntity partnerPermission = new PartnerPermissionEntity();
        partnerPermission.setUpdatePersonId(getUserIdStr());
        partnerPermission.setUpdatePersonName(getUserName());
        partnerPermission.setUpdateTime(new Date());
        partnerPermission.setDeleteFlag(String.valueOf(DeleteFlagEnum.DELETE.getCode()));
        boolean flag = partnerPermissionService.removeBatch(Arrays.asList(permissionIds), partnerPermission);
        return flag ? R.ok() : R.error();
    }

    @RequestMapping("/queryList")
    public R queryList(String roleId){
        List<PartnerPermissionEntity> permissionList = partnerPermissionService.queryList(new PartnerPermissionEntity());
        List<String> permissionIdList = partnerRolePermissionService.getRolePermissionStrByRoleId(roleId);
        if(CollectionUtils.isNotEmpty(permissionList)){
            for(PartnerPermissionEntity partnerPermissionEntity : permissionList){
                if(permissionIdList.contains(partnerPermissionEntity.getPermissionIdText())){
                    partnerPermissionEntity.setPermissionChecked(true);
                }
            }
        }
        return R.ok().put("permissionList", permissionList);
    }

}
